var app = getApp();
var util = require('../../utils/util.js');
var address = app.globalData.address;
var that;
Page({
  data: {
    begin:1,
    splist:[],
    load:true,
    phone: null,
    typeid:null,
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    swiperList: [{content:'所有'}],
    
  },
  mallinformationJump: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'subPackages/mall-details/mall-details?informationid=' + id,
      //url: '../mall-details/index'
    })
  },
  stopTouchMove: function () {
    return false;
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current,
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    var typeid;
    if (e.target.dataset.typeid){
      typeid = e.target.dataset.typeid;
    }else{
      typeid='';
    }
    this.setData({
      typeid: typeid
    })
    if (this.data.currentTab == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
        let splist = that.data.splist;
        splist.commodityList = [];
        this.setData({
          begin: 0,
          totalPageCount:1,
          splist: splist
        })
 
      this.onReachBottom();
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 3) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },

  //网页加载即调用
  onLoad:function(options){
    that = this;
    app.getUserInfo(function (r) {
      // that.showList();
    })

    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        that.setData({
          winHeight: calc
        });
      }
    });
  },
  onshow:function(){
    if (wx.getStorageSync('check') || wx.getStorageSync('count') == 0) {

      that.setData({
        splist: [],
      })
      wx.setStorageSync('count', 1);
      wx.setStorageSync('check', false);
      that.showList();
    }
  },
  /**
* 生命周期函数--监听页面初次渲染完成
*/
  onReady: function () {
    //获得dialog组件
    this.dialog = this.selectComponent("#dialog");
  },
  //网页加载即显示数据
  showList:function(e){
    let url = address + 'Shopping/toShopping';
    let params = { 'companyId':wx.getStorageSync('companyId'),
      'empId': wx.getStorageSync('empId')
    };
    util.requestLoading(url, params,'正在加载中',function(res){
      let list = res;
      console.log(list)
      if (list.dictionaryList){
        that.setData({
          swiperList: that.data.swiperList.concat(list.dictionaryList)
        })
        console.log(that.data.swiperList)
      }
      if (JSON.stringify(list) == "{}"){
        that.setData({
          splist: list
        })
        wx.showToast({
          title: '商城暂未开启，请联系客服',
          icon: 'none',
          duration: 300000
        })
         
      }else{
        wx.hideToast();
        that.setData({
          splist: list,
          totalPageCount: list.totalPageCount
        })
      }
      
    },function(){
      console.log('查询失败')
    });
  },
  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom: function () {
    // console.log('上拉触底了')
    let begin = ++that.data.begin;
    that.setData({
      begin: begin
    })
    if (that.data.begin <= that.data.totalPageCount) {
      wx.showLoading({
        title: '加载中',
      })
      // 请求后台，获取下一页的数据。
      let url = address + 'commodity/showCommodity';
      let params = {
        'shoppingId': that.data.splist.id,
        'empId': wx.getStorageSync('empId'),
        'begin': that.data.begin
      };
      if (that.data.typeid) {
        params.typeId = that.data.typeid;
      }
      // console.log(params)
      util.requestLoading(url, params, '正在加载中', function (res) {
        // console.log(res)
        if (res.totalPageCount){
          that.setData({
            totalPageCount: res.totalPageCount
          })
        }
        let list = res.myList;
        if (list.length>0){
          let splist = that.data.splist;
          let commodityList = splist.commodityList.concat(list);
          splist.commodityList = commodityList
          that.setData({
            splist: splist
          })
        }else{
          // console.log(list)
          wx.showToast({
            title: '暂无数据',
            icon:'none',
            duration:10000
          })
        }
        
      }, function () {
        console.log('查询失败')
      });
    } else {
      wx.showLoading({
        title: '加载中',
      })
      setTimeout(function () {
        wx.hideLoading();
        wx.showToast({
          title: '没有更多了',
          icon: 'none',
          duration: 1200
        })
      }, 500)

    }
  },

  // getPhoneNumber: function (e) {
  //   console.log(e)
  //   if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  //     wx.showModal({
  //       title: '提示',
  //       showCancel: false,
  //       content: '手机号码未授权',
  //       success: function (res) {
  //         wx.navigateTo({
  //           url: 'subPackages/chat/chat',
  //         })
  //       }
  //     })
  //   } else {
  //     console.log(e.detail.errMsg)
  //     that.mobileDecode(e.detail.iv, e.detail.encryptedData);//破解手机号码
  //   }
  //   let url = address + 'Interaction/chat';
  //   let params = { businessId: wx.getStorageSync('bId')};
  //   let sessionId = app.globalData.sessionId;
  //   util.nomeRequest(url, params, sessionId, function (res) {
  //     if(res == 1){
  //       console.log('记录用户打开聊天成功！')
  //     }
  //   }, function () { });
  // },
  // // 破解手机号码
  // mobileDecode: function (iv, encryptedData) {
  //   let url = address + 'Tool/mobileDecode';
  //   let params = {
  //     openId: wx.getStorageSync('openId'),
  //     sessionKey: wx.getStorageSync('sessionKey'),
  //     encryptedData: encryptedData,
  //     iv: iv,
  //   };
  //   util.request(url, params, function (res) {
  //     app.globalData.phone = res; 
  //     wx.setStorageSync('phone', res)
  //     wx.navigateTo({
  //       url: 'subPackages/chat/chat',
  //     })
  //   }, function () { });

  // },
  // toChat: function () {
  //   wx.navigateTo({
  //     url: 'subPackages/chat/chat',
  //   })
  // },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    
    let url = address + 'Shopping/toShopping';
    let params = { 'companyId': wx.getStorageSync('companyId') };
    util.request(url, params, function (res) {
      let list = res;
      console.log(list)
      if (JSON.stringify(list) == "{}") {
        that.setData({
          splist: list
        })
        wx.showToast({
          title: '商城暂未开启，请联系客服',
          icon: 'none',
          duration: 300000
        })

      } else {
        wx.hideToast();
        that.setData({
          
          currentTab:0,
          begin:1,
          totalPageCount: list.totalPageCount,
          splist: list
        })
      }

    }, function () {
      console.log('查询失败')
    });
    setTimeout(function () {
      
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();

      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 1000)

  },

// scroll-view触底事件
  bindscrolltolower:function(e){
    this.onReachBottom();
  },

  // testSubmit: function (e) {
  //   util.testSubmit(e);
  // },
  footerTap: app.footerTap
})